package com.neiquan.neiquandemo.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.neiquan.neiquandemo.R;

import java.lang.reflect.Field;

import butterknife.ButterKnife;

/**
 * 作者 ： 邓勇军
 * 时间 ： 2016/12/29.
 * version:1.0
 */
public abstract class BaseFragment extends Fragment {

    /**
     * 根元素
     */
    protected View rootView;
    /**
     * 布局渲染工具
     */
    protected LayoutInflater mInflater;
    protected Context mContext;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        if (null != rootView) {
//            ViewGroup parent = (ViewGroup) rootView.getParent();
//            if (null != parent) {
//                parent.removeView(rootView);
//            }
//        } else {
        mInflater = inflater;
        mContext = inflater.getContext();
        rootView = mInflater.inflate(getRootViewId(), null);
        ButterKnife.inject(this, rootView);
        setViews();
        initData();
//        }
        return rootView;
    }


    /**
     * 初始化View,加载布局
     *
     * @return 布局
     */
    public abstract int getRootViewId();

    /**
     * 解决fragment嵌套问题:No activity
     */
    @Override
    public void onDetach() {
        super.onDetach();
        try {
            Field childFragmentManager = Fragment.class
                    .getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);

        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 初始化数据
     */
    public abstract void setViews();

    /**
     * 加载网络数据
     */
    public abstract void initData();


    protected void setTitleTv(int id) {
        TextView tv = (TextView) rootView.findViewById(R.id.title_tv);
        tv.setText(id);
    }

    protected void setHeadVisibility(int visibility) {
        rootView.findViewById(net.neiquan.applibrary.R.id.head_view).setVisibility(visibility);
    }

    protected void setTitleTv(String str) {
        TextView tv = (TextView) rootView.findViewById(R.id.title_tv);
        tv.setText(str);
    }

//    protected void setBackTv(String str) {
//        TextView tv = (TextView) view.findViewById(R.id.back_tv);
//        tv.setText(str);
//    }

    protected void setNextTv(String str) {
        TextView tv = (TextView) rootView.findViewById(R.id.next_tv);
        tv.setVisibility(View.VISIBLE);
        tv.setText(str);
        View iv = rootView.findViewById(R.id.next_img);
        iv.setVisibility(View.GONE);
    }

    protected TextView getNextTv() {

        return (TextView) rootView.findViewById(R.id.next_tv);
    }

    protected void setBackGone() {
        View tv = rootView.findViewById(R.id.ly_back);
        tv.setVisibility(View.GONE);
    }




    @SuppressLint("NewApi")
    protected void setNextTvBG(Drawable drawable) {
        TextView tv = (TextView) rootView.findViewById(R.id.next_tv);
        tv.setBackground(drawable);
    }

    protected void setNextImage(int id) {
        TextView tv = (TextView) rootView.findViewById(R.id.next_tv);
        tv.setVisibility(View.GONE);
        ImageView iv = (ImageView) rootView.findViewById(R.id.next_img);
        iv.setVisibility(View.VISIBLE);
        iv.setImageResource(id);
    }
    protected void setNextSearchImage(int id) {
        TextView tv = (TextView) rootView.findViewById(R.id.next_tv);
        tv.setVisibility(View.GONE);
        ImageView iv = (ImageView) rootView.findViewById(R.id.searh_next_img);
        iv.setVisibility(View.VISIBLE);
        iv.setImageResource(id);
    }
    protected void setTitleOnClick(View.OnClickListener onClick) {
        TextView tv = (TextView) rootView.findViewById(R.id.title_tv);
        tv.setOnClickListener(onClick);
    }
    protected void setNextOnClick(View.OnClickListener onClick) {
        TextView tv = (TextView) rootView.findViewById(R.id.next_tv);
        tv.setOnClickListener(onClick);
        View iv = rootView.findViewById(R.id.next_img);
        iv.setOnClickListener(onClick);
    }
    protected void setNextSearchOnClick(View.OnClickListener onClick) {
        View iv = rootView.findViewById(R.id.searh_next_img);
        iv.setOnClickListener(onClick);
    }
    protected void setNextGone() {
        TextView tv = (TextView) rootView.findViewById(R.id.next_tv);
        tv.setVisibility(View.GONE);
        ImageView iv = (ImageView) rootView.findViewById(R.id.next_img);
        iv.setVisibility(View.GONE);
    }

}
